//
//  BTSaleListVC.m
//  BaxTer
//
//  Created by wsj2015 on 16/10/14.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "BTSaleListVC.h"
//#import "BTTopicListCell.h"
//#import "BTTopicDetailVC.h"
#import "BTChooseCourseVC.h"
#import "BTCourseModel.h"
#import "BTCourseDetailVC.h"
#import "CWQ_courseIntroductionCell.h"
#import "CWQ_historySearchVC.h"
@interface BTSaleListVC ()

@property (nonatomic,  strong) BTCourseQueryTypeModel *courseCondtionModel;




@end

@implementation BTSaleListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
 
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)configUI{

    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"CWQ_courseIntroductionCell" bundle:nil] forCellReuseIdentifier:@"CWQ_courseIntroductionCell"];
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-64);
    UIBarButtonItem *right1 =[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"筛选_icon.png"] style:0 target:self action:@selector(repick)];
     UIBarButtonItem *right2 =[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"搜索_icon.png"] style:0 target:self action:@selector(toSearch)];
   
    self.navigationItem.rightBarButtonItems =@[right2,right1];
    
    weakSelf_define;
    [self.tableView wsj_HeaderRefreshBlock:^{
         [weakSelf requestSaleCourseList:YES];
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        [weakSelf requestSaleCourseList:NO];
    }];
       [self requestSaleCourseList:YES];
    
    
}
- (void)repick{
    weakSelf_define;
    [BTChooseCourseVC showWithVC:self.navigationController WithDoneBlock:^(BTCourseQueryTypeModel *typeModel) {
        weakSelf.courseCondtionModel = typeModel;
        [weakSelf requestSaleCourseList:YES];
    }];

}
- (void)toSearch{
    CWQ_historySearchVC *vc =[CWQ_historySearchVC new];
    vc.searchType = CWQ_historySearchVCType_Course_FlagSale;
    vc.flagId = self.courseflagId;
    [self.navigationController pushViewController:vc animated:YES];
   

}

- (void)requestSaleCourseList:(BOOL)isRecover{
    static NSInteger  page =1;
    if (!BT_USERID) {
        //        return;
    }
    if (isRecover) {
        page = 1;
    }else{
        page ++;
        
    }
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    
    params[@"page"] = @(page);
    params[@"rows"] = @(10);

 
    params[@"id"] = @(self.courseflagId);
    weakSelf_define;
    self.tableView.loading = YES;
    if (self.courseCondtionModel) {
        [params addEntriesFromDictionary:[self.courseCondtionModel toDictionary]];
    }
   
   
    [[BTNetworkManager sharedMg] POST:kSaleCourseList parameters:params success:^(NSDictionary *responseObject) {
        weakSelf.tableView.loading = NO;
        [weakSelf.tableView.footer endRefreshing];
        [weakSelf.tableView.header endRefreshing];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
                //总页数
               
                NSInteger totalPage;
              
                if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]] ) {
                    totalPage = 0;
                }else{
                    totalPage = [responseObject[@"data"][@"totalPage"] integerValue];;
                }
                
                //                总条数
                NSInteger currentPage = [responseObject[@"data"][@"page"] integerValue];
                
                
                if (currentPage >=totalPage) {
                    [weakSelf.tableView.footer noticeNoMoreData];
                    
                }else{
                    [weakSelf.tableView.footer resetNoMoreData];
                    
                }
                if (isRecover) {
                    [weakSelf.dataArray removeAllObjects];
                    
                }else{
                    
                }
                
                [weakSelf.dataArray addObjectsFromArray:[BTCourseModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]]];
                
                
            }
            
        }
        
        
        [weakSelf.tableView reloadData];
        
        
    } failure:^(NSString *err) {
        weakSelf.tableView.loading = NO;
        [weakSelf.tableView.footer endRefreshing];
        [weakSelf.tableView.header endRefreshing];
    }];
    

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CWQ_courseIntroductionCell *cell  =[tableView dequeueReusableCellWithIdentifier:@"CWQ_courseIntroductionCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.courseModel = self.dataArray[indexPath.row];
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BTCourseDetailVC *vc =[BTCourseDetailVC new];
    BTCourseModel *model = self.dataArray[indexPath.row];
    vc.courseId = [model.id integerValue];
   [self.navigationController pushViewController:vc animated:YES];
   [self.navigationController setNavigationBarHidden:YES animated:YES];

    
}
@end
